package com.andy.qi.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.andy.qi.conf.RoomServerConf;
import com.andy.qi.domain.GameServerInfo;
import com.andy.qi.service.RoomConf;
import com.andy.qi.service.RoomLocation;
import com.andy.qi.service.RoomMgr;
import com.andy.qi.service.TokenMgr;
import com.andy.qi.util.Json;
import com.andy.qi.util.StrUtils;

@CrossOrigin(origins = "*")
@RestController
public class RoomController extends WebApi {
    
    @Autowired private RoomMgr roomMgr;
    @Autowired private RoomServerConf config;
    
    @Autowired private TokenMgr tokenMgr;
    @Autowired GameServerInfo gameServerInfo;
    
    @RequestMapping("/get_server_info")
    Object getServerInfo(String serverId, String sign) {
    	System.out.println("serverId:" + serverId);
    	System.out.println("sign:" + sign);
        if(!config.getServerId().equals(serverId)) {
            return fail("invalid parameters");
        }
        
        String md5 = StrUtils.md5(serverId + config.getRoomPriKey());
        if(!md5.equals(sign)) {
            return fail("sign check failed.");
        }
        Map<String, RoomLocation> locations = roomMgr.getUserLocations();
        List<String> arr = new ArrayList<>();
        for(String userId : locations.keySet()){
            String roomId = locations.get(userId).roomId;
            arr.add(userId);
            arr.add(roomId);
        }
        Map<String, Object> ret = result();
        ret.put("userroominfo", arr);
        return succ(ret);
    }
    
    @RequestMapping("/create_room")
    Object createRoom(String userid, String sign, Integer gems, String conf) {
        String md5 = StrUtils.md5(userid + conf + gems + config.getRoomPriKey());
        if(!md5.equals(sign)){
            return fail("sign check failed.");
        }
        
        RoomConf roomConf = Json.fromJson(conf, RoomConf.class);
        WebResult result = roomMgr.createRoom(userid,roomConf,gems,gameServerInfo.serverIp,config.getClientPort());
        
        if(result.getErrcode() == 0 && result.getErrmsg() != null) {
            Map<String, Object> ret = result();
            ret.put("roomid", result.getErrmsg());
            return succ(ret);
        } else {
            return fail("create failed.");
        }
    }
    @RequestMapping("/enter_room")
    Object enterRoom(String userid, String sign, String name, String roomid) {
        System.out.println("userid:" + userid);
        System.out.println("sign:" + sign);
        System.out.println("roomId:" + roomid);
        System.out.println("name:" + name);
        String md5 = StrUtils.md5(userid + name + roomid + config.getRoomPriKey());
        if(!md5.equals(sign)){
            return fail("sign check failed.");
        }

        //安排玩家坐下
        int result = roomMgr.enterRoom(roomid,userid,name);
        if(result != 0){
            if(result == 1){
                return fail("room is full.");
            }
            else if(result == 2){
                return fail("can't find room.");
            }   
            return fail("enter room fail.");
        }
        
        String token = tokenMgr.createToken(userid, 5000);
        Map<String, Object> ret = result();
        ret.put("token", token);
        return succ(ret);
    }
    
    
    @RequestMapping("/is_room_runing")
    Object isRoomRuning(String roomid, String sign, String name, String roomId) {
        String md5 = StrUtils.md5(roomid + config.getRoomPriKey());
        if(!md5.equals(sign)){
            return fail("sign check failed.");
        }
        
        Map<String, Object> ret = result();
        ret.put("runing", true);
        return succ(ret);
    }
    
    
}